<?php
/**
 * An order object (Datamapper)
 *
 * @author Ishtiaque Shahrier <md.ishtiaque@newgenbd.net>
 * @package order
 * @subpackage models
 */
class order_m_order extends a_base_model_datamapper {

    public function __construct($id=null) {
        $this->table = 'orders';
        $this->has_many = array(
        );
        $this->has_one = array(
        );
        $this->has_many['address_m_order'] = array(
            'class' => 'address_m_order',
            'join_other_as' => 'address',
            'join_self_as' => 'order'
        );
        $this->has_many['extra_data_m_order'] = array(
            'class' => 'extra_data_m_order',
            'join_other_as' => 'extra',
            'join_self_as' => 'order'
        );
        $this->has_one['user_m_user'] = array(
            'class' => 'user_m_user',
            'join_other_as' => 'user',
            'join_self_as' => 'order'
        );
        $this->has_many['order_item_m_order'] = array(
            'class' => 'order_item_m_order',
            'join_other_as' => 'item',
            'join_self_as' => 'order'
        );
        parent::__construct($id);
    }

    public function mark_as_paid($payment_driver_id){
        $this->status = status_m_order::COMPLETE;
        $this->payment_driver_id = $payment_driver_id;
        $this->save();
    }

    public function set_shipping($carier_id,$cost){
        $this->shipping_carrier_id = $carier_id;
        $this->shipping_cost = $cost;
        $this->save();
    }

    public function reset_shipping(){
        $this->shipping_carrier_id = NULL;
        $this->shipping_cost = 0.0;
        $this->save();

    }

    public function get_billing_address(){
        $srv = new service_l_order();
        $billing =  $srv->get_billing_address($this->id);
        unset($srv);
        return $billing;

    }
    public function get_shipping_address(){
        $srv = new service_l_order();
        $billing =  $srv->get_shiping_address($this->id);
        unset($srv);
        return $billing;

    }

    public function set_email_sent($flag=0){
        $this->email_sent = $flag;
        $this->save();
    }

    public function get_user_obj(){
        $user = new user_m_user($this->user_id);
        return $user->exists() ? $user->get() : NULL;
    }
    public function get_items(){
        
    }

    
}
?>